package com.iflysse.services.impl;

import com.iflysse.dao.ResourceFeedbackDao;

import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.common.PageBeanBoResp;
import com.iflysse.pojo.bo.req.DealResourceFeedbackBoReq;
import com.iflysse.pojo.bo.req.ResourceFeedbackBoReq;
import com.iflysse.pojo.bo.resp.ResourceFeedbackBoResp;
import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.common.PageBeanPoReq;
import com.iflysse.pojo.po.req.DealResourceFeedbackPoReq;
import com.iflysse.pojo.po.req.ResourceFeedbackConditionPoReq;
import com.iflysse.pojo.po.resp.ResourceFeedbackPoResp;
import com.iflysse.services.ResourceFeedbackService;
import com.iflysse.util.CommonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ResourceFeedbackServiceImpl implements ResourceFeedbackService {
    public static final Logger log = Logger.getLogger(ActivityServiceImpl.class);
    @Autowired
    ResourceFeedbackDao resourceFeedbackDao;
    /**
     * 根据课程编号,反馈类型,处理状态获取资源反馈
     * 分页处理,请求需要提供分页信息
     * @param resourceFeedbackBoReq
     * @return
     */
    @Override
    public PageBeanBoResp<ResourceFeedbackBoResp> getResourceFeedbackByCondition(ResourceFeedbackBoReq resourceFeedbackBoReq) throws SQLException {
        ResourceFeedbackConditionPoReq resourceFeedbackConditionPoReq = new ResourceFeedbackConditionPoReq() ;
        resourceFeedbackConditionPoReq = CommonUtil.objToObj(resourceFeedbackBoReq, ResourceFeedbackConditionPoReq.class);
        resourceFeedbackConditionPoReq.setPageBeanPoReq(CommonUtil.objToObj(resourceFeedbackBoReq.getPageBeanBoReq(), PageBeanPoReq.class));
        //获取符合条件的总反馈数
        Long total = resourceFeedbackDao.getTotalAmountByCondition(resourceFeedbackConditionPoReq);
        //获取反馈列表
        List<ResourceFeedbackPoResp> resourceFeedbackByConditionAndPage = resourceFeedbackDao.getResourceFeedbackByConditionAndPage(resourceFeedbackConditionPoReq);
        int pageNum = resourceFeedbackBoReq.getPageBeanBoReq().getPageIndex();
        int pageSize = resourceFeedbackBoReq.getPageBeanBoReq().getPageSize();
        Long totalRecord = total;
        //封装到page对象
        PageBeanBoResp<ResourceFeedbackBoResp> pageBeanBoResp
                = new PageBeanBoResp<ResourceFeedbackBoResp>(pageNum,pageSize,(int)total.longValue());
        List<ResourceFeedbackBoResp> list = new ArrayList<>();
        resourceFeedbackByConditionAndPage.forEach(resourceFeedbackPoResp -> {
            list.add(CommonUtil.objToObj(resourceFeedbackPoResp, ResourceFeedbackBoResp.class));
        });
        //将数据存入page对象中
        pageBeanBoResp.setList(list);

        return pageBeanBoResp;
    }

    /**
     * 处理资源反馈(1已处理,0未处理)
     *
     * @param dealResourceFeedbackBoReq
     * @return
     */
    @Override
    public CommonBoResp dealResourceFeedback(DealResourceFeedbackBoReq dealResourceFeedbackBoReq) throws SQLException {
        DealResourceFeedbackPoReq dealResourceFeedbackPoReq = CommonUtil.objToObj(dealResourceFeedbackBoReq, DealResourceFeedbackPoReq.class);
        CommonPoResp commonPoResp = new CommonPoResp();
        int i = resourceFeedbackDao.dealResourceFeedback(dealResourceFeedbackPoReq);
        CommonUtil.printToPoResult(commonPoResp,i);
        CommonBoResp commonBoResp = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
        return commonBoResp;
    }
}
